<?php
$lang=$_REQUEST['lang'];
$dfrom=$_REQUEST['dfrom'];
$nguoiguidi=$_REQUEST['nguoiguidi'];
$company=$_REQUEST['company'];
$dto=$_REQUEST['dto'];
$dname=$_REQUEST['dname'];

if (empty($lang) || $lang == "EN") {
	$value = "value/value_en.php";
} else if($lang == "VN") {
	$value = "value/value_vn.php";
}
else {
	$value = "value/value_kr.php";
}
include($value);
include ("sa_funcs.php");
include 'connection.php';
$query="select em.Email,em.IDEmployee as emid,
em.FullName as emname,
ct.id,
ct.employeeID,
upsuser.FullName as upname ,
cruser.FullName as crname ,
ct.employeeName,ct.time_work,
ct.time_in,ct.time_out,
ct.is_late,
ct.is_comeback_early,
ct.half_day_off,
ct.all_day_off,
ct.overtime,
ct.note,
ct.user_create,ct.createDate,
ct.user_update,
ct.update_date 
from sa_check_time ct left join employee em on ct.employeeID = em.EmployeeCode 
left join  employee upsuser on upsuser.IDEmployee = ct.user_update 
left join employee cruser on cruser.IDEmployee = ct.user_create 
where ct.`status`!=1 and '".$dfrom."' <= ct.time_work 
and ct.time_work <= '".$dto."' ";
if($dname!="all"){
	$query .=" and em.FullName like '%".$dname."%'";
}
$query .=" order by ct.employeeID,ct.time_work asc";
$mkq=mysql_query($query);

$sendarr=array();
if(mysql_num_rows($mkq)>0){
	while($row=mysql_fetch_array($mkq)){
		$subarr=array();
		$subarr[emid]=$row[emid];
		$subarr[Email]=$row[Email];
		$subarr[emname]=$row[emname];
		$subarr[employeeID]=$row[employeeID];
		$subarr[time_work]=$row[time_work];
		$subarr[time_in]=$row[time_in];
		$subarr[time_out]=$row[time_out];
		$subarr[is_late]=$row[is_late];
		$subarr[is_comeback_early]=$row[is_comeback_early];
		$subarr[half_day_off]=$row[half_day_off];
		$subarr[all_day_off]=$row[all_day_off];
		$subarr[overtime]=$row[overtime];
		$subarr[note]=$row[note];
		$subarr[note]=$row[note];
		if($row[upname]!=""){
			$subarr[upname]=$row[upname];
			$subarr[uptime]=$row[update_date];
		}else{
			$subarr[upname]=$row[crname];
			$subarr[uptime]=$row[createDate];
		}
		
		$sendarr[$row[emid]][]=$subarr;
	}
}
require_once('class.phpmailer.php');
$mthu=array();
$mthu[0]='<font color="red" ><b>'.$l_vday_chunhat.'</b></font>';
$mthu[1]=$l_vday_thuhai;
$mthu[2]=$l_vday_thuba;
$mthu[3]=$l_vday_thutu;
$mthu[4]=$l_vday_thunam;
$mthu[5]=$l_vday_thusau;
$mthu[6]='<font color="red" ><b>'.$l_vday_thubay.'</b></font>';
$mthu[7]=" ";

foreach($sendarr as $k => $giatri){
	$mail = new PHPMailer();

	$mail->IsSMTP();
	$mail->SMTPDebug  = 2;                    
	$mail->SMTPAuth   = true;                  
	$mail->SMTPSecure = "ssl";
	$mail->Host       = "smtp.gmail.com";     
	$mail->Port       = 465;                     
	$mail->Username   = "elisoftmailserver@gmail.com"; 
	$mail->Password   = "1234567892727";    
	$mail->SetFrom('elisoftsolution@gmail.com',$company);
	
	$mail->AddAddress($giatri[0][Email], $giatri[0][emname]);
	
	$mail->Subject    =$l_working_spreadsheet." : ".$dfrom."  -  ".$dto;
	
	$mail->CharSet = "utf-8";
	$mail->IsHTML(true);
	$body="";
	$body ='<table width="100%"><tr><td style="padding:10px;color:black; font-weight:bold; font-size:13px;text-align:center;">'.$l_working_spreadsheet.' : '.$dfrom.'  -  '.$dto.'</td></tr></table>';
	$body .='<table style="width:100%;border:1px #000000 solid;">';
		$body .='<tr style="background:#EEEEEE;font-weight:bold;text-align:center;border-bottom:1px #000000 solid;">';
			$body .='<td width="10px" style="border-left: 1px #000000 dotted;border-right: 1px #000000 dotted;border-bottom: 1px #000000 dotted">'.$l_no.'</td>';
			$body .='<td width="15%" style="border-right: 1px #000000 dotted;border-bottom: 1px #000000 dotted">'.$l_employee_name.'</td>';
			$body .='<td width="5%" style="border-right: 1px #000000 dotted;border-bottom: 1px #000000 dotted">'.$l_code.'</td>';
			$body .='<td width="5%" style="border-right: 1px #000000 dotted;border-bottom: 1px #000000 dotted">'.$l_thu.'</td>';
			$body .='<td width="10%" style="border-right: 1px #000000 dotted;border-bottom: 1px #000000 dotted">'.$l_date.'</td>';
			$body .='<td width="8%" style="border-right: 1px #000000 dotted;border-bottom: 1px #000000 dotted">'.$l_timecheckin.'</td>';
			$body .='<td width="8%" style="border-right: 1px #000000 dotted;border-bottom: 1px #000000 dotted">'.$l_timecheckout.'</td>';
			$body .='<td width="5%" style="border-right: 1px #000000 dotted;border-bottom: 1px #000000 dotted">'.$l_late.'</td>';
			$body .='<td width="5%" style="border-right: 1px #000000 dotted;border-bottom: 1px #000000 dotted">'.$l_comebackearly.'</td>';
			$body .='<td width="5%" style="border-right: 1px #000000 dotted;border-bottom: 1px #000000 dotted">'.$l_half_dayoff.'</td>';
			$body .='<td width="5%" style="border-right: 1px #000000 dotted;border-bottom: 1px #000000 dotted">'.$l_all_dayoff.'</td>';
			$body .='<td width="5%" style="border-right: 1px #000000 dotted;border-bottom: 1px #000000 dotted">'.$l_overtime.'</td>';
			$body .='<td width="10%" style="border-right: 1px #000000 dotted;border-bottom: 1px #000000 dotted">'.$l_note.'</td>';
			$body .='<td width="15%" style="border-right: 1px #000000 dotted;border-bottom: 1px #000000 dotted">'.$l_update_information.'</td>';
		$body .='</tr>';
		for($i=0;$i<count($giatri);$i++){
			$thu=getWday($giatri[$i][time_work]);
			if($thu==6 || $thu==0){
				$body .='<tr style="text-align:center;background:#ccffcc;">';
			}else{
				$body .='<tr style="text-align:center">';
			}
				$body .='<td width="10px" style="border: 1px #000000 dotted">'.($i+1).'</td>';
				$body .='<td width="15%" style="border: 1px #000000 dotted">'.$giatri[$i][emname].'</td>';
				$body .='<td width="5%" style="border: 1px #000000 dotted">'.$giatri[$i][employeeID].'</td>';
				$body .='<td width="5%" style="border: 1px #000000 dotted">'.$mthu[$thu].'</td>';
				$body .='<td width="10%" style="border: 1px #000000 dotted">'.$giatri[$i][time_work].'</td>';
				$body .='<td width="8%" style="border: 1px #000000 dotted">'.$giatri[$i][time_in].'</td>';
				$body .='<td width="8%" style="border: 1px #000000 dotted">'.$giatri[$i][time_out].'</td>';
				if($giatri[$i][is_late]==1){
					$body .='<td width="5%" style="background:orange;font-weight:bold;border: 1px #000000 dotted;text-align:center;">X</td>';
				}else{
					$body .='<td width="5%" style="border: 1px #000000 dotted"></td>';
				}
				if($giatri[$i][is_comeback_early]==1){
					$body .='<td width="5%" style="background:yellow;font-weight:bold;border: 1px #000000 dotted;text-align:center;">X</td>';
				}else{
					$body .='<td width="5%" style="border: 1px #000000 dotted;text-align:center;"></td>';
				}
				//$body .='<td class="rf-dt-c" width="5%">'.$giatri[$i][is_comeback_early].'</td>';
				if($giatri[$i][half_day_off]==1){
					$body .='<td width="5%" style="background:pink;font-weight:bold;border: 1px #000000 dotted;text-align:center;">X</td>';
				}else{
					$body .='<td width="5%" style="border: 1px #000000 dotted"></td>';
				}
				//$body .='<td class="rf-dt-c" width="5%">'.$giatri[$i][half_day_off].'</td>';
				if($giatri[$i][all_day_off]==1){
					$body .='<td width="5%" style="background:red;font-weight:bold;border: 1px #000000 dotted;text-align:center;">X</td>';
				}else{
					$body .='<td width="5%" style="border: 1px #000000 dotted"></td>';
				}
				//$body .='<td class="rf-dt-c" width="5%">'.$giatri[$i][all_day_off].'</td>';
				if($giatri[$i][overtime]==1){
					$body .='<td width="5%" style="background:green;font-weight:bold;border: 1px #000000 dotted;text-align:center;">X</td>';
				}else{
					$body .='<td width="5%" style="border: 1px #000000 dotted"></td>';
				}
				
				$body .='<td width="10%" style="border: 1px #000000 dotted;text-align:center;">'.$giatri[$i][note].'</td>';
				$body .='<td width="15%" style="border: 1px #000000 dotted;text-align:center;">'.$giatri[$i][upname].'<br>'.$giatri[$i][uptime].'</td>';
			$body .='</tr>';
		}
		
	$body .='</table>';
	$body .='<br><table width="100%" style="padding:20px" >
	<tr><td align="right"><b>'.$l_sender.':</b> <b><i>'.$nguoiguidi.'</i></b></td></tr>
	<tr><td align="right">(Bảng chấm công có sai xót gì thì liên hệ với người chấm công ngay. Sau ngày này sẽ không giải quyết mọi thắc mắc về bảng chấm công nữa)</td></tr>
	</table>';
	//echo $body;
	$mail->Body = $body;
	$mail->AltBody=$dtitle;
	
	if(!$mail->Send()) {
		echo "<br><p style='width:100%;'>".$giatri[0][emname]." -> ".$l_sendmail_fail." ". $mail->ErrorInfo."</p>";
	} else {
		echo "<br><p style='width:100%;'>".$l_sendmail_ok." ".$giatri[0][emname]."</p>";
	}
}
mysql_close($connection);
?>